package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _114搜索插入位置
 * @description
 * @date 2023/08/14 22:09
 * <p>
 * ******************************************************
 */
public class _114搜索插入位置 {

    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = right - (right - left) / 2;
            int num = nums[mid];
            if (num > target) {
                right = mid - 1;
            } else if (num < target) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return left;
    }
}
